$(document).ready(function(){
	mobileMenu();
    pagingStyle();
    preparingTopPage();
    ajaxSendMail();
});

function mobileMenu(){
    var mmenu = {
        _label    : $('#mobile-btn label'),
        _checkbox : $('.nav-trigger'),
        _sidebar  : $('.mobile-sidebar'),
        _wrap     : $('.mobile-sidebar-wrap')
    };
    var ani = {
        _out       : 'sidebar-out',
        _in        : 'sidebar-in',
        _fade_time : 80
    };

    mmenu._label.click(function(){
        mmenu._sidebar.fadeIn(ani._fade_time);
        mmenu._wrap.addClass(ani._out);
    });
    mmenu._sidebar.click(function() {
        mmenu._wrap.addClass(ani._off);
        mmenu._sidebar.fadeOut(ani._fade_time);
    });
    mmenu._wrap.click(function(e) {
        e.stopPropagation();
    });
}

function pagingStyle(){
    //set mask
    var mask_height = $('.site-wrap').height()-200;
    var mask_width = $(window).width();
    var mask_bg = 'url(' + $('#mask').attr('data-bg') + ') ' + $('#mask').attr('data-bg-property');
    $('#mask').css({'height':mask_height, 'width':mask_width, 'background': mask_bg}).hide().fadeIn();


    $('.blog-artical-pagenate ul li a > span').parent().css({'background': '#FFF'});
    var filterList = {
    	init: function () {
    		$('#portfoliolist').mixitup({
    			targetSelector: '.portfolio',
    			filterSelector: '.filter',
    			effects: ['fade'],
    			easing: 'snap',
    			onMixEnd: filterList.hoverEffect()
    		});
    	},
    	hoverEffect: function () {
    	}
    };
    filterList.init();

    //scroll top
    $('#toTop').click(function(e){
        e.preventDefault();
        $("body").animate({ scrollTop: 0 }, 600);
        return false;
    });
    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });

    if($('.popup-with-zoom-anim').length>0){
        $('.popup-with-zoom-anim').magnificPopup({
            type:'image',
            gallery: {
                enabled: true
            },
            image: {
                titleSrc: 'title'
            },
            zoom: {
                enabled: true,
                duration: 300,
                easing: 'ease-in-out',
                opener: function(openerElement) {
                    return openerElement.is('img') ? openerElement : openerElement.find('img');
                }
            }
        });
    }
}

function ajaxSendMail(){
    var frm_contact = $('#frm-contact');
    var target = $('#target');
    $('#btn-contact-submit').click(function(){
        $.ajax({
            url: frm_contact.attr('action'),
            method: 'POST',
            cache: false,
            data: frm_contact.serialize(),
            beforeSend:function(){
                loading();
                $('#to-progress').trigger('click');
            },
            success: function(data){
                setMessage(data, '#frm-contact .input');
                $.magnificPopup.close();
            }
        });
    });
}

function setMessage(data, clearItem){
    //set title
    $('#message #message-title').html(data.msg);
    if(data.code==0){
        //set state
        $('#message').removeClass('alert-success').addClass('alert-danger').removeClass('hidden');
        var liItem = '';
        $.each(data.details, function(i, item){
            liItem += "<li>" + item.key + " " + item.etext + "</li>";
        });
        $('#message #message-content').html(liItem);
    } else {
        $('#message').addClass('alert-success').removeClass('alert-danger').removeClass('hidden');
        $('#message #message-content').html('');
        $(clearItem).val('');
    }
}

function loading(){
    $('#to-progress').magnificPopup({
        removalDelay: 500,
        mainClass: 'mfp-fade'
    });
}

function preparingTopPage(){
    if($("#flexiselDemo3").length!=0){
        $("#flexiselDemo3").flexisel({
            visibleItems: 3,
            animationSpeed: 1000,
            autoPlay: false,
            autoPlaySpeed: 3000,
            pauseOnHover: true,
            enableResponsiveBreakpoints: true,
            responsiveBreakpoints: {
                portrait: {
                    changePoint:480,
                    visibleItems: 1
                },
                landscape: {
                    changePoint:640,
                    visibleItems: 2
                },
                tablet: {
                    changePoint:768,
                    visibleItems: 3
                }
            }
        });
    }

    if($('#da-slider').length>0){
        $('.da-slide').each(function(index){
            var img = $(this).attr('data-image');
            $(this).css('background', 'url('+img+') no-repeat center center').removeAttr('data-image');
        });
        $('#da-slider').cslider({
            autoplay    : true,
            bgincrement : 450
        });
    }

    //service grid
    var min_height = 0;
    $('.services-grid').each(function(i, j){
        var cur_height = $(this).height();
        if(cur_height > min_height){
            min_height = cur_height;
        }
    });
    $('.services-grid').css('min-height', min_height);
    $('.services-grids span.summary-item-view-more').click(function(){
        var viewMore = $(this);
        var hiddenContent = $(this).parents('.services-grid-right').find('.summary-item');
        $('.services-grids .summary-item').slideUp();
        $('.services-grids span.summary-item-view-more span').html(viewMore.attr('data-more'));
        if(hiddenContent.is(':hidden')){
            hiddenContent.slideDown();
            viewMore.find('span').html(viewMore.attr('data-less'));
            viewMore.attr('data-state', 1);
        } else {
            hiddenContent.slideUp();
            viewMore.find('span').html(viewMore.attr('data-more'));
            viewMore.attr('data-state', 0);
        }
    });
}